package com.tanhua.dubbo.api;


import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Map;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取用户喜欢的人的数据列表
/*    public List findLoverList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> likeUserList = mongoTemplate.find(query, UserLike.class);
        query.addCriteria(Criteria.where("userId").in(likeUserList));
        long eachLovecount = mongoTemplate.count(query, Long.class);
        return null;
    }*/

    @Override
    public Map counts(Long userId) {
        return null;
    }

    @Override
    public Long countLovers(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
        .and("isLike").is(true));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public List<UserLike> findByUserId(Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        return list;
    }

    @Override
    public void deleteById(Long uid) {
        Query query = Query.query(Criteria.where("likeUserId").is(uid)
                        .and("isLike").is(true));
        mongoTemplate.remove(query,UserLike.class);
    }
}
